package common.core.domain.udp;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;

import java.net.InetSocketAddress;

@Data
public class UdpApiProtocol {

    private ChannelHandlerContext ctx;
    private InetSocketAddress remoteAddress;
    private JSONObject buffer;

    private String apiName;
    private String protocol;
    private String method;
    private String version;

    private String remoteIP;
    private int remotePort;
    private String localIP;
    private int localPort;


    public UdpApiProtocol(ChannelHandlerContext ctx, InetSocketAddress remoteAddress, JSONObject buffer){
        this.ctx = ctx;
        this.remoteAddress = remoteAddress;
        this.buffer = buffer;

        String remoteAddrStr = remoteAddress.toString();

        String[] remoteAddr = remoteAddrStr.split(":");

        this.remoteIP = remoteAddr[0].replace("/","");
        this.remotePort = Integer.parseInt(remoteAddr[1]);

        InetSocketAddress localAddress = (InetSocketAddress)ctx.channel().localAddress();
        String localAddrStr = localAddress.toString();
        this.localIP = localAddrStr.split(":")[0].replace("/","");
        this.localPort =Integer.parseInt(localAddrStr.split(":")[1]);

        this.apiName = buffer.getString("apiName");
        this.protocol = buffer.getString("protocol");
        this.method = buffer.getString("method");
        this.version = buffer.getString("version");
    }
}
